﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General

Namespace DoAnQuanLyBanHang.DTO
    Public Class XuatHangData
        Private m_XuatHangData As New DataService()
        Private m_MatHangData As New DataService()
        Public Function LayMatHang() As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Mat_Hang")
            m_MatHangData.Load(cmd)
            Return m_MatHangData
        End Function
        Public Function LayXuatHang() As DataTable
            Dim cmd As New SqlCommand("SELECT * FROM Xuat_Hang")
            m_XuatHangData.Load(cmd)
            Return m_XuatHangData
        End Function
        Public Function LayDSXuatHangTheoMa(MaPhieu As [String]) As DataTable
            Dim cmd As New SqlCommand("SELECT XH.MaPhieu, MH.MaMH, KH.MaKH, KH.HoTen, NV.MaNV, NV.HoTenNV, MH.TenMH, LT.LoaiTien, LGD.TenLoaiGiaoDich, XH.SoLuong, XH.GiaBan, XH.ThanhTien, XH.NgayXuat FROM Xuat_Hang XH INNER JOIN Mat_Hang MH ON MH.MaMH = XH.MaMH INNER JOIN Khach_Hang KH ON KH.MaKH = XH.MaKH INNER JOIN Nhan_Vien NV ON NV.MaNV = XH.MaNV INNER JOIN Loai_Tien LT ON LT.MaLoaiTien = XH.MaLoaiTien INNER JOIN Loai_Giao_Dich LGD ON LGD.MaLoaiGiaoDich = XH.MaLoaiGiaoDich WHERE XH.MaPhieu = @MaPhieu")
            cmd.Parameters.Add("MaPhieu", SqlDbType.VarChar).Value = MaPhieu
            m_XuatHangData.Load(cmd)
            Return m_XuatHangData
        End Function

        Public Function ThemDongMoi() As DataRow
            Return m_XuatHangData.NewRow()
        End Function

        Public Sub ThemXuatHang(m_Row As DataRow)
            m_XuatHangData.Rows.Add(m_Row)
        End Sub

        Public Function LuuXuatHang() As Boolean
            Return m_XuatHangData.ExecuteNoneQuery() > 0
        End Function

        Public Sub LuuXuatHang(maphieu As [String], mamh As [String], makh As [String], manv As [String], soluong As Single, giaban As Single, _
         thanhtien As Single, ngayxuat As DateTime, maloaitien As [String], maloaigd As [String])
            Dim cmd As New SqlCommand("INSERT INTO Xuat_Hang VALUES(@maphieu, @mamh, @makh, @manv, @soluong, @giaban, @thanhtien, @ngayxuat, @maloaitien, @maloaigd)")
            cmd.Parameters.Add("maphieu", SqlDbType.VarChar).Value = maphieu
            cmd.Parameters.Add("mamh", SqlDbType.VarChar).Value = mamh
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = makh
            cmd.Parameters.Add("manv", SqlDbType.VarChar).Value = manv
            cmd.Parameters.Add("soluong", SqlDbType.Float).Value = soluong
            cmd.Parameters.Add("giaban", SqlDbType.Float).Value = giaban
            cmd.Parameters.Add("thanhtien", SqlDbType.Float).Value = thanhtien
            cmd.Parameters.Add("ngayxuat", SqlDbType.DateTime).Value = ngayxuat
            cmd.Parameters.Add("maloaitien", SqlDbType.VarChar).Value = maloaitien
            cmd.Parameters.Add("maloaigd", SqlDbType.VarChar).Value = maloaigd
            m_XuatHangData.Load(cmd)
        End Sub

        Public Sub SuaXuatHang(maphieu As [String], mamh As [String], makh As [String], manv As [String], soluong As Single, giaban As Single, _
         thanhtien As Single, ngayxuat As DateTime, maloaitien As [String], maloaigd As [String])
            Dim cmd As New SqlCommand("UPDATE Xuat_Hang SET soluong = @soluong, giaban = @giaban, thanhtien = @thanhtien, ngayxuat = @ngayxuat, maloaitien = @maloaitien, maloaigiaodich = @maloaigd WHERE maphieu = @maphieu AND mamh = @mamh AND makh = @makh AND manv = @manv")
            cmd.Parameters.Add("maphieu", SqlDbType.VarChar).Value = maphieu
            cmd.Parameters.Add("mamh", SqlDbType.VarChar).Value = mamh
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = makh
            cmd.Parameters.Add("manv", SqlDbType.VarChar).Value = manv
            cmd.Parameters.Add("soluong", SqlDbType.Float).Value = soluong
            cmd.Parameters.Add("giaban", SqlDbType.Float).Value = giaban
            cmd.Parameters.Add("thanhtien", SqlDbType.Float).Value = thanhtien
            cmd.Parameters.Add("ngayxuat", SqlDbType.DateTime).Value = ngayxuat
            cmd.Parameters.Add("maloaitien", SqlDbType.VarChar).Value = maloaitien
            cmd.Parameters.Add("maloaigd", SqlDbType.VarChar).Value = maloaigd
            m_XuatHangData.Load(cmd)
        End Sub

        Public Sub XoaXuatHang(maphieu As [String], mamh As [String], makh As [String], manv As [String])
            Dim cmd As New SqlCommand("DELETE FROM Xuat_Hang WHERE maphieu = @maphieu AND mamh = @mamh AND makh = @makh AND manv = @manv")
            cmd.Parameters.Add("maphieu", SqlDbType.VarChar).Value = maphieu
            cmd.Parameters.Add("mamh", SqlDbType.VarChar).Value = mamh
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = makh
            cmd.Parameters.Add("manv", SqlDbType.VarChar).Value = manv
            m_XuatHangData.Load(cmd)
        End Sub
    End Class
End Namespace
